package com.exam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.mapper.ExamManageMapper;
import com.exam.pojo.ApiResult;
import com.exam.pojo.ExamManage;
import com.exam.service.ExamManageService;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class ExamManageController {
    @Autowired
    private ExamManageService examManageService;
    //不分页查询所有试卷
    @GetMapping("/exams")
    public ApiResult findAll(){
       ApiResult apiResult=
               ApiResultHandler.buildApiResult(200,"查询成功"
                       ,examManageService.findAll());
    return apiResult;
    }
    //分页查询
    @GetMapping("/exams/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page,
                             @PathVariable Integer size){
        Page<ExamManage> examManagePage = new Page<>(page, size);
        IPage<ExamManage> all = examManageService.findAll(examManagePage);
        return ApiResultHandler.buildApiResult(200,"查询成功",all);
    }
    //根据id查找
    @GetMapping("/exam/{examCode}")
    public ApiResult findById(@PathVariable Integer examCode){
        ExamManage examManage =examManageService.findById(examCode);
        if (examManage==null){
            return ApiResultHandler.buildApiResult(7587,"考试编码不存在",examManage);
        }
        return ApiResultHandler.buildApiResult(200,"Id查询成功",examManage);
    }
    //删除操作
    @DeleteMapping("/exam/{examCode}")
    public ApiResult deleteById(@PathVariable Integer examCode){
         int result =examManageService.deleteById(examCode);
        return ApiResultHandler.buildApiResult(200,"根据Id删除成功",result);
    }
    //更新修改操作
    @PutMapping("/exam")
    public ApiResult updateById(@RequestBody ExamManage examManage){
        int result =examManageService.updateById(examManage);
        return ApiResultHandler.buildApiResult(200,"修改成功",result);
    }
    //考试新增
    @PostMapping("/exam")
    public ApiResult insertBy(@RequestBody ExamManage examManage){
        int result =examManageService.insertBy(examManage);
        if (result==1) {
            return ApiResultHandler.buildApiResult(200, "添加成功", result);
        }else {
            return ApiResultHandler.buildApiResult(400, "添加失败", result);
        }
    }
    @GetMapping("/examManagePaperId")
    public ApiResult examManagePaperId(){
        ExamManage result = examManageService.ManagePaperId();
        if (result!=null){
            return ApiResultHandler.buildApiResult(200, "查看成功", result);
        }else {
            return ApiResultHandler.buildApiResult(400, "查看失败", result);
        }
    }
}
